import AntModal from "@/components/pt/dialog/AntModal";
import DeptTree from "../../../system/user/modules/DeptTree.vue";
import {
  getBizAssessInfo,
  addBizAssessInfo,
  updateBizAssessInfo,
  findMaxSort,
} from "@/api/biz/bizAssessInfo";
import { listDeptTree } from "@/api/system/dept";
export default {
  name: "CreateForm",
  props: {
    typeOptions: {
      type: Array,
      required: true,
    },

    statusOptions: {
      type: Array,
      required: true,
    },
    adjustSize: {
      type: Boolean,
      default: true,
    },
  },
  components: {
    AntModal,
    DeptTree,
  },
  data() {
    return {
      open: false,
      spinning: false,
      delayTime: 100,
      labelCol: { span: 4 },
      wrapperCol: { span: 14 },
      loading: false,
      total: 0,
      id: undefined,
      formTitle: "添加考核信息",
      deptOptions: [],
      expandedKeys: [],
      setDataOptionInfo: [],
      clickDeptNode: [],
      // 表单参数
      form: {},
      rules: {
        title: [
          { required: true, message: "考核标题不能为空", trigger: "blur" },
        ],
        endTime: [
          { required: true, message: "截止时间不能为空", trigger: "blur" },
        ],
        type: [
          { required: true, message: "考核类型不能为空", trigger: "blur" },
        ],
      },
    };
  },
  filters: {},
  created() {
    this.getTreeselect();
  },
  computed: {},
  watch: {},
  mounted() {},
  methods: {
    /** 查询部门下拉树结构 */
    getTreeselect() {
      listDeptTree("0", 3).then((response) => {
        this.deptOptions = response.data;
        this.getExpandedKeys(this.deptOptions, 3);
      });
    },
    getExpandedKeys(nodes, expandLevel) {
      if (expandLevel > 1) {
        // 最后一层不展开
        if (nodes != null) {
          nodes.forEach((node) => {
            this.expandedKeys.push(node.id);
            expandLevel = expandLevel - 1;
            return this.getExpandedKeys(node.children, expandLevel);
          });
        }
      }
    },
    onClose() {
      this.open = false;
      this.reset();
      this.$emit("close");
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
      this.$emit("close");
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        title: undefined,

        endTime: undefined,

        type: undefined,

        sort: undefined,

        status: undefined,
      };
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      /** 获取最大编号 */
      findMaxSort().then((response) => {
        this.form.sort = response.data;
        this.formTitle = "添加考核信息";
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.open = true;
      this.spinning = !this.spinning;
      const bizAssessInfoId = row.id;
      getBizAssessInfo(bizAssessInfoId).then((response) => {
        this.form = response.data;
        this.formTitle = "修改考核信息";
        this.spinning = !this.spinning;
      });
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs.form.validate((valid) => {
        if (valid) {
          if (this.form.id !== undefined) {
            const saveForm = JSON.parse(JSON.stringify(this.form));
            updateBizAssessInfo(saveForm).then((response) => {
              this.$message.success("修改成功", 3);
              this.open = false;
              this.$emit("ok");
              this.$emit("close");
            });
          } else {
            this.form.status = 0;
            const saveForm = JSON.parse(JSON.stringify(this.form));
            addBizAssessInfo(saveForm).then((response) => {
              this.$message.success("新增成功", 3);
              this.open = false;
              this.$emit("ok");
              this.$emit("close");
            });
          }
        } else {
          return false;
        }
      });
    },
    back() {
      const index = "/biz/assessinfo/index";
      this.$router.push(index);
    },
  },
};
